//
//  NRWCarNavigationView.m
//  New_Runway
//
//  Created by mack on 2018/1/22.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//
#import "UIView+DDAddition.h"
#import "NRWCarNavigationView.h"

@implementation NRWCarNavigationView
{
    NSString *_titleStr;
    UITapGestureRecognizer *_headTapGesture;
    CGFloat _y;
}
-(instancetype)initWithController:(UIViewController *)myselfController andTitle:(NSString *)title andStyle:(navgationStyle)myStyle{
    _y = height_navbar;
    self = [super initWithFrame:CGRectMake(0, 0, Screen_width, _y)];
    
    
    if (self) {
        self.myselfController = myselfController;
        _lineView = [UIView lineViewForColor:MAIN_NAV_COLOR() AndFrame:CGRectMake(0, self.frame.size.height - 0.5, Screen_width, 0.5)];
        [self addSubview:_lineView];
        
        _leftItem = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftItem setAdjustsImageWhenHighlighted:NO];
        [_leftItem.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_leftItem addTarget:self action:@selector(leftItemClick) forControlEvents:UIControlEventTouchUpInside];
        [_leftItem setFrame:CGRectMake(2, self.height - 44, 40, 44)];
        [_leftItem setTitleColor:MAIN_FONT_NORMAL_COLOR() forState:UIControlStateNormal];
        
        [_leftItem setImage:[UIImage imageNamed:@"nav_common_back_n"] forState:UIControlStateNormal];
        [self addSubview:_leftItem];
        
        _rightItem = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightItem.layer.cornerRadius = 2;
        _rightItem.clipsToBounds = YES;
        [_rightItem addTarget:self action:@selector(rightItemClick) forControlEvents:UIControlEventTouchUpInside];
        [_rightItem setFrame:CGRectMake(self.width - 65, self.height - 45, 60, 45)];
        [_rightItem setTitleColor:kColorWhite() forState:UIControlStateNormal];
        [_rightItem.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [self addSubview:_rightItem];
        
        
        _titleStr = title;
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, self.height - 35, Screen_width - (80 * 2), 25)];
        _titleLabel.numberOfLines = 1;
        _titleLabel.backgroundColor = [UIColor clearColor];
        [_titleLabel setFont:[UIFont systemFontOfSize:17]];
        [_titleLabel setTextAlignment:NSTextAlignmentCenter];
        _titleLabel.textColor = MAIN_FONT_COLOR();
        [_titleLabel setText:_titleStr];
        _titleLabel.userInteractionEnabled = YES;
        [self addSubview:_titleLabel];
        
        _headTapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(titleLabelTap)];
        [_headTapGesture setNumberOfTapsRequired:2];
        [_headTapGesture setNumberOfTouchesRequired:1];
        [_titleLabel addGestureRecognizer:_headTapGesture];
        
        [self changeMyNavgationStyle:myStyle];
        
    }
    return self;
}
-(void)changeMyNavgationStyle:(navgationStyle)myStyle{
    self.MyNavgationStyle = myStyle;
    switch (_MyNavgationStyle) {
        case navgationStyle_deepColor:
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
            self.backgroundColor = MAIN_NAV_COLOR();
            _titleLabel.textColor = kColorWhite();
            [_leftItem setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            
            break;
            
        case navgationStyle_lightColor:
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
             self.backgroundColor = MAIN_NAV_COLOR();
            _titleLabel.textColor = kColorWhite();
            [_leftItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
}

- (void)setTitle:(NSString *)title
{
    if (![_titleStr isEqualToString:title]) {
        _titleStr = title;
        [_titleLabel setFrame:CGRectMake(80, self.height - 35, Screen_width - (80 * 2), 25)];
        [_titleLabel setText:_titleStr];
    }
    
}
//导航栏左边按钮
-(void)setupleftItemImage:(UIImage *)leftImage{
    [_leftItem setImage:leftImage forState:UIControlStateNormal];
    [_leftItem setImage:leftImage forState:UIControlStateHighlighted];
    [_leftItem setTitle:nil forState:UIControlStateNormal];
    if (!_leftItem.superview) {
        [self addSubview:_leftItem];
    }
}
-(void)setupLeftItemImage:(UIImage *)leftImage newRect:(CGRect)rect{
    [_leftItem setFrame:rect];
    [self setupleftItemImage:leftImage];
}
-(void)setupLeftItemTitle:(NSString *)leftTitle{
    if (!leftTitle) {
        return;
    }
    CGSize _size = [leftTitle sizeWithAttributes:@{NSFontAttributeName:kFontSize(16)}];
    [_leftItem setImage:nil forState:UIControlStateNormal];
    [_leftItem setImage:nil forState:UIControlStateHighlighted];
    [_leftItem setFrame:CGRectMake(0, self.height - 44, _size.width + 20, 44)];
    [_leftItem setTitle:leftTitle forState:UIControlStateNormal];
    [self addSubview:_leftItem];
}
//导航栏右边边按钮
-(void)setupRightItemImage:(UIImage *)rightImage{
    
    [_rightItem setImage:rightImage forState:UIControlStateNormal];
    [_rightItem setImage:rightImage forState:UIControlStateHighlighted];
    [_rightItem setTitle:nil forState:UIControlStateNormal];
    if (!_rightItem.superview) {
        [self addSubview:_rightItem];
    }
}
-(void)setupRightItemImage:(UIImage *)RightImage newRect:(CGRect)rect{
    [_rightItem setFrame:rect];
    [self setupRightItemImage:RightImage];
}
-(void)setupRightItemTitle:(NSString *)rightTitle{
    if (!rightTitle) {
        return;
    }
    CGSize _size = [rightTitle sizeWithAttributes:@{NSFontAttributeName:kFontSize(16)}];
    [_rightItem setImage:nil forState:UIControlStateNormal];
    [_rightItem setImage:nil forState:UIControlStateHighlighted];
    int btnWidth = _size.width + 5;
    [_rightItem setFrame:CGRectMake(Screen_width - 8 - btnWidth, self.height - 44, btnWidth, 44)];
    [_rightItem setTitle:rightTitle forState:UIControlStateNormal];
    [self addSubview:_leftItem];
    
}

-(void)leftItemClick{
    if (_myselfController) {
        if ([_myselfController respondsToSelector:@selector(leftItemClick)]) {
            [((id)_myselfController) leftItemClick];
        }else{
            [_myselfController.navigationController popViewControllerAnimated:YES];
            _myselfController.tabBarController.tabBar.hidden = YES;
        }
    }
}
-(void)rightItemClick{
    if (_myselfController) {
        if ([_myselfController respondsToSelector:@selector(rightItemClick)]) {
            [((id)_myselfController) rightItemClick];
        }else{
            
            
        }
    }
}
-(void)titleLabelTap{
    
    if (_myselfController) {
        if ([_myselfController respondsToSelector:@selector(titleLabelTap)]) {
            [((id)_myselfController) titleLabelTap];
        }else{
            
            
        }
    }
    
}
@end
